/*
 * 1294. Power of Three
 * Given an integer, write a function to determine if it is a power of three.
 * 
 * 挑战
 * Could you do it without using any loop / recursion?
 * 
 * 2018.06.12 @jeyming
 */
package power_of_three_1294;

public class Power_of_three_1294 {
    /**
     * @param n: an integer
     * @return: if n is a power of three
     */
    public static boolean isPowerOfThree(int n) {
        // Write your code here
    	boolean judge = false;
    	while(n > 0) {
    		if(n == 1) {
    			judge = true;
    			break;
    		}
    		if((n % 3 != 0) && (n != 1)) {
    			break;
    		}
    		if((n % 3 == 0) && (n/3 == 1)) {
    			judge = true;
    		}
    		n /= 3;
    	}
        return judge;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(isPowerOfThree(0));
		System.out.println(isPowerOfThree(1));
		System.out.println(isPowerOfThree(2));
		System.out.println(isPowerOfThree(3));
		System.out.println(isPowerOfThree(9));

	}

}
